// System includes
#include <QPalette>
#include <QDebug>
#include <QPlastiqueStyle>

// Local includes
#include "yigchat.h"
#include "graphics/yigcolorkit.h"
#include "yig.h"

extern QString USER_NAME;

YigChat::YigChat(Yig *yig, QObject *parent) :
    mYig(yig),
    QObject(parent)
{
    QPlastiqueStyle yigStyle;
    QFont chatFont("monaco", 11);

    displayWidget = new QWidget();
    displayWidget->setGeometry(5, 5, 390, 265);
    displayWidget->setFixedWidth(390);
    displayWidget->setFixedHeight(265);
    displayWidget->setWindowOpacity(0.5);
    displayWidget->setAttribute(Qt::WA_TransparentForMouseEvents);
    displayWidget->setWindowFlags( Qt::CustomizeWindowHint | Qt::Tool);

    QColor translucentBackground = YigColorKit::focus1;
    translucentBackground.setAlpha(0.1);

    QPalette windowPalette = displayWidget->palette();
    windowPalette.setColor(QPalette::Window, YigColorKit::background2);
    displayWidget->setPalette(windowPalette);
    displayWidget->setStyle(&yigStyle);

    chatDisplay = new QTextEdit(displayWidget);
    chatDisplay->setGeometry(0, 0, 390, 265);
    chatDisplay->setFrameStyle(QFrame::NoFrame);
    chatDisplay->setFont(chatFont);

    QPalette displayPalette = chatDisplay->palette();
    displayPalette.setColor(QPalette::Window, YigColorKit::background2);
    displayPalette.setColor(QPalette::Base, YigColorKit::background2);
    displayPalette.setColor(QPalette::Background, YigColorKit::background2);
    displayPalette.setColor(QPalette::Text, YigColorKit::focus1Highlight);
    displayPalette.setColor(QPalette::WindowText, YigColorKit::focus1Highlight);
    //displayPalette.setColor(QPalette::Button, YigColorKit::background2);
    chatDisplay->setPalette(displayPalette);
    chatDisplay->setStyle(&yigStyle);
    chatDisplay->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    entryWidget = new QWidget();
    entryWidget->setGeometry(5, 275, 390, 20);
    entryWidget->setFixedWidth(390);
    entryWidget->setFixedHeight(20);
    entryWidget->setFocusPolicy(Qt::ClickFocus);
    entryWidget->setWindowOpacity(0.8);
    entryWidget->setWindowFlags( Qt::CustomizeWindowHint | Qt::Tool);

    windowPalette = entryWidget->palette();
    windowPalette.setColor(QPalette::Base, YigColorKit::background2);
    windowPalette.setColor(QPalette::Text, YigColorKit::focus1);
    windowPalette.setColor(QPalette::WindowText, YigColorKit::focus1);
    windowPalette.setColor(QPalette::Window, YigColorKit::background2);
    windowPalette.setColor(QPalette::Highlight, YigColorKit::background2);
    entryWidget->setPalette(windowPalette);
    entryWidget->setStyle(&yigStyle);

    chatEntry = new QLineEdit(entryWidget);
    chatEntry->setGeometry(0, 0, 390, 20);
    chatEntry->setFrame(false);
    chatEntry->setFont(chatFont);

    QPalette entryPalette = chatEntry->palette();
    entryPalette.setColor(QPalette::Window, YigColorKit::background2);
    entryPalette.setColor(QPalette::Base, YigColorKit::background2);
    entryPalette.setColor(QPalette::Highlight, YigColorKit::background2);
    entryPalette.setColor(QPalette::Light, YigColorKit::background2);
    entryPalette.setColor(QPalette::Text, YigColorKit::focus1);
    entryPalette.setColor(chatEntry->backgroundRole(), Qt::transparent);
    chatEntry->setPalette(entryPalette);
    chatEntry->setStyle(&yigStyle);

    chatDisplay->setText("Yig, The Father of Serpents\n\n");

    QObject::connect(chatEntry, SIGNAL(returnPressed()), this, SLOT(submitChatLine()));
}

YigChat::~YigChat()
{
    delete displayWidget;
    delete entryWidget;
}

void YigChat::moveElements(int x, int y)
{
    displayWidget->move(x, y);
    entryWidget->move(x, y+275);
}

void YigChat::showElements()
{
    displayWidget->show();
    entryWidget->show();
}

void YigChat::hideElements()
{
    displayWidget->hide();
    entryWidget->hide();
}

void YigChat::grabKeyboardFocus()
{
    qDebug() << "FOCUS!";
}

void YigChat::receiveMessage(QString message)
{
    chatDisplay->moveCursor( QTextCursor::End, QTextCursor::MoveAnchor );
    chatDisplay->insertPlainText( message+"\n" );
    chatDisplay->ensureCursorVisible();
}

void YigChat::submitChatLine()
{
    //QString line = USER_NAME + ": " + chatEntry->text();
    //receiveMessage( line );
    QByteArray ba = chatEntry->text().toLocal8Bit();
    mYig->sendCthulhu("/chat", ba.data());
    /* here we send the text across the network */
    //client->sendChat( line );

    /* finally we clear the ChatInput */
    chatEntry->clear();
}
